package com.dingpeikeji.quickdeer.modules.wx.day;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FRentproductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;

@Controller
@RequestMapping("${wxPath}/day")
public class WxDayController {
	@Autowired
	private FRentproductService rentproductService;
	@Autowired
	private TRentproductService tRentproductService;
	@Autowired
	private TRentgroupService tRentgroupService;
	@Autowired
	private TSitedtlService tSitedtlService;

	@RequestMapping("dayResult")
	public String getDayRentList(TRentproduct tRentproduct, HttpServletRequest request, HttpServletResponse response,
			Model model, TCargotype tCargotype, String pageNo, String pageSize,String daysnum) throws Exception {
		//根据默认地址查询城市id
		String siteid = tSitedtlService.getSiteIdByName(request.getParameter("departcity.sitename"));
		tCargotype.getDepartcity().setSiteid(siteid);
		tRentproduct.getDepartcity().setSiteid(siteid);
		//包车天数处理
		if (daysnum.contains(",")) {
			if ("跨市".equals(tRentproduct.getTabType())) {
				tRentproduct.setDaysnum(Integer.parseInt(daysnum.split(",")[0]));
			}else{
				if ("0.5".equals(daysnum.split(",")[1])) {//市内0.5
					tRentproduct.setDaysnum(0);
				}else{
					tRentproduct.setDaysnum(Integer.parseInt(daysnum.split(",")[1]));
				}
			}
		}else{
			tRentproduct.setDaysnum(Integer.parseInt(daysnum));
		}
		// 日期处理
		String fromdate = request.getParameter("fromdatestr");
		String[] arr = fromdate.split("\\s");
		tRentproduct.setFromdateStr(arr[0]);
		tRentproduct.setTime(arr[1]);
		// 获取车型
		tCargotype.setTstyles(request.getParameter("tstyle"));
		tRentproduct.setCargotype(tCargotype);
		// 如果发车日期为空或者小于当前日期，则设置成明天 包车天数为空，1天-10天选择，默认1天
		tRentproduct.handleFromDate(model).handleDaysnum();
		// 如果是未登录或者是普通用户身份（非企业用户）则只可查看面向公众的整车
		rentproductService.setCompAgreerateOffice(tRentproduct, request, "f");
		// 分页对象
		Page<TCargotype> page = new Page<TCargotype>(request, response);
		tCargotype.setPage(page);
		page.setPageSize(Integer.parseInt(pageSize));
		page.setPageNo(Integer.parseInt(pageNo));
		// 跨市range=2,市内range=1(t_rentgroup表)
		if (tRentproduct.getTabType().equals("市内")) {
			tCargotype.setRange("1");
		} else {
			tCargotype.setRange("2");
		}
		List<TCargotype> cargotypeList = rentproductService.findCargotypeList(tCargotype);
		page.setList(cargotypeList);
		// 获取车型id,查询对应的线路报价
		List<TRentproduct> rentproductList = null;
		if (cargotypeList.size() > 0) {
			for (TCargotype tc : cargotypeList) {
				tRentproduct.setCargotype(tc);
				// 市内日租包车
				if (TOrders.ORDER_TYPE_RENTPRODUCT_DAY.equals(tRentproduct.getRentType())
						&& tRentproduct.getTabType().equals("市内")) {
					rentproductList = rentproductService.findInCityList(tRentproduct);
					if (tRentproduct.getDaysnum() == 0) {// 0.5天,计算价格
						for (TRentproduct rp : rentproductList) {
							rp.setSuitrtnprice(Math.rint(rp.getSuitrtnprice() * 0.8));
						}

					}
				}
				// 跨城日租包车
				if (TOrders.ORDER_TYPE_RENTPRODUCT_DAY.equals(tRentproduct.getRentType())
						&& tRentproduct.getTabType().equals("跨市")) {
					rentproductList = rentproductService.findBetweenCityList(tRentproduct);
				}
				tc.setRentproductList(rentproductList);
			}
			tRentproduct.setFromdateStr(fromdate);
			model.addAttribute("cargotypeList", cargotypeList);
			model.addAttribute("tstyle", request.getParameter("tstyle"));
			model.addAttribute("rentproductList", rentproductList);
			model.addAttribute("tRentproduct", tRentproduct);
			return "wx/modules/dayProduct/dayResult";
		} else {
			model.addAttribute("cargotypeList", "");
			return "wx/modules/dayProduct/dayResult";
		}
	}

	@RequestMapping("booking")
	public String details(TRentproduct tRentproduct, Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 根据ID查询选中的整车信息
		TRentproduct rentproduct = tRentproductService.get(tRentproduct.getRentid());
		TRentgroup rentgroup = tRentgroupService.get(rentproduct.getRentgrpid());
		String destcityPart = "";
		if (rentproduct != null) {
			// 协议价逻辑处理
			TRentproduct query = new TRentproduct();
			query.setRentid(tRentproduct.getRentid());
			query.setDepartdate(DateUtils.parseDate(tRentproduct.getFromdateStr()));
			// 如果是未登录或者是普通用户身份（非企业用户）则只可查看面向公众的整车
			rentproductService.setCompAgreerateOffice(query, request, "f");
			query = rentproductService.getSuitrtnprice(query);
			if ("跨市".equals(tRentproduct.getTabType())) {
				destcityPart = "跨市";
			} else {
				destcityPart = "市内";
			}
			rentproduct.setSuitrtnprice(query.getSuitrtnprice());
			if (tRentproduct.getDaysnum() == 0) {
				rentproduct.setProductname(rentproduct.getDepartcity().getSitename() + destcityPart + "0.5日按天包车");
				rentproduct.setSuitrtnprice(Math.rint(query.getSuitrtnprice()*0.8*1));
				model.addAttribute("totalPrice",Math.rint(query.getSuitrtnprice()*0.8*1));
			} else {
				rentproduct.setProductname(
						rentproduct.getDepartcity().getSitename() + destcityPart + tRentproduct.getDaysnum() + "日按天包车");
				model.addAttribute("totalPrice", rentproduct.getSuitrtnprice()*tRentproduct.getDaysnum());
			}
		}
		model.addAttribute("tRentproduct", rentproduct);
		model.addAttribute("rentgroup", rentgroup);
		model.addAttribute("query", tRentproduct);
		return "wx/modules/dayProduct/dayBooking";
	}
}
